package com.codedancer.campus.manager.handler.impl.circle;

import static com.codedancer.campus.enums.SystemMessageType.DYNAMIC_PUBLISH;

import com.codedancer.campus.common.utils.UUIDGenerator;
import com.codedancer.campus.enums.SystemMessageType;
import com.codedancer.campus.manager.handler.SystemMessageHandler;
import com.codedancer.campus.manager.service.model.SystemMessageMqParamModel;
import com.codedancer.campus.mapper.SystemMessageMapper;
import com.codedancer.campus.mapper.UserFollowMapper;
import com.codedancer.campus.mapper.UserMapper;
import com.codedancer.campus.po.SystemMessage;
import com.codedancer.campus.po.User;
import com.codedancer.campus.po.UserFollow;
import java.util.List;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Data
@Component
@Slf4j
public class DynamicPublishSystemMessageHandler implements SystemMessageHandler {

    @Autowired
    private SystemMessageMapper systemMessageMapper;
    @Autowired
    private UserFollowMapper userFollowMapper;
    @Autowired
    private UserMapper userMapper;
    private final SystemMessageType messageType = DYNAMIC_PUBLISH;

    @Override
    public void saveSystemMessage(SystemMessageMqParamModel systemMessageMqParamModel) {
        log.info("收到动态发布推送粉丝系统消息，{}", systemMessageMqParamModel);
        List<UserFollow> userFollows = userFollowMapper.selectListByColumn("followed_user_uuid",
            systemMessageMqParamModel.getDynamicPublishUserUuid());
        User user = userMapper.selectById(systemMessageMqParamModel.getDynamicPublishUserUuid());
        for (UserFollow userFollow : userFollows) {
            SystemMessage dynamicPublishSystemMessage = SystemMessage.builder()
                .uuid(UUIDGenerator.getUUID())
                .receiveUserUuid(userFollow.getFollowUserUuid())
                .isRead(false)
                .messageType(DYNAMIC_PUBLISH)
                .content("你关注的【" + user.getNickName() + "】，发布了新的动态，快去他主页看看吧")
                .build();
            systemMessageMapper.insert(dynamicPublishSystemMessage);
        }
    }

    @Override
    public SystemMessageType getSystemMessageType() {
        return messageType;
    }
}
